<?php
/**
 * Description: 获取名师解析试卷
 * User: devforma
 * Date: 2016/11/14
 * Time: 16:51
 */

class TeacherInterviewPapers implements iController {
    const NUM_PER_PAGE = 15;

    /**
     * api请求处理主函数
     * @param array $queryArgs 就是$_REQUEST变量
     * @return array api返回结果，必须含response_code
     */
    public function output($queryArgs) {

        global $cmsdb;

        if (empty($queryArgs['page']) || filter_var($queryArgs['page'], FILTER_VALIDATE_INT) === false || $queryArgs['page'] <= 0) {
            return genOutput(1000);
        }

        $start = ($queryArgs['page'] - 1) * self::NUM_PER_PAGE;

        $sql = "SELECT `id`, `name` FROM `interview_papers` ORDER BY CONVERT(`name` USING gbk) COLLATE gbk_chinese_ci ASC LIMIT {$start}, ".self::NUM_PER_PAGE;
        $papers = (array)$cmsdb->getResults($sql);

        $country = $others = [];
        foreach ($papers as $p) {
            if ($p->interview_area_id == 2) { //国家
                $country[] = [
                    'id'   => (int)$p->id,
                    'name' => $p->name
                ];
            } else {
                $others[] = [
                    'id'   => (int)$p->id,
                    'name' => $p->name
                ];
            }
        }

        return genOutput(1, ['papers' => array_merge($country, $others)]);
    }
}